//
//  NSObject+KDTool.m
//  iOSKdUser
//
//  Created by WYC on 2018/1/2.
//  Copyright © 2018年 KD. All rights reserved.
//

#import "NSObject+Tool.h"
#import "AppDelegate.h"
#define WeakObj(o) __weak typeof(o) o##Weak = o

@implementation NSObject (Tool)

+(NSString *)checkStringNull:(id)object
{
    if ([object isKindOfClass:[NSNull class]]) {
        return @"";
    }
    
    if (object == nil) {
        return @"";
    }
    if ([[NSString stringWithFormat:@"%@",object] isEqualToString:@"null"]) {
        return @"";
    }
    if ([[NSString stringWithFormat:@"%@",object] isEqualToString:@"(null)"]) {
        return @"";
    }
    return [NSString stringWithFormat:@"%@",object];
    
}
+(BOOL)checkStringIsNull:(id _Nullable )object
{
    if ([object isKindOfClass:[NSNull class]]) {
        return YES;
    }
    
    if (object == nil) {
        return YES;
    }
    if ([[NSString stringWithFormat:@"%@",object] isEqualToString:@"null"]) {
        return YES;
    }
    if ([NSString stringWithFormat:@"%@",object].length == 0) {
        return YES;
    }
    if ([[NSString stringWithFormat:@"%@",object] isEqualToString:@"(null)"]) {
        return YES;
    }
    return NO;
}


-(NSString *)clearCityName
{
    if (![self isKindOfClass:[NSString class]]) {
        return (id)self;
    }
    NSString *string = [((NSString *)self) stringByReplacingOccurrencesOfString:@"市" withString:@""];
    return string;
}

-(NSAttributedString *)changeAttributedString:(NSArray *)rangesArray attributes:(NSArray *)attributes
{
    if (![self isKindOfClass:[NSString class]]) {
        return nil;
    }
    // 设置标签文字
    NSMutableAttributedString *attrituteString = [[NSMutableAttributedString alloc] initWithString:(NSString *)self];
    
    for (int i = 0; i< rangesArray.count; i++) {
        // 获取标红的位置和长度
        NSRange range = [rangesArray[i] rangeValue];
        // 设置标签文字的属性
        [attrituteString setAttributes:attributes[i] range:range];
    }
    // 显示在Label上
    return attrituteString;
}


// 显示在Label上
+(NSMutableDictionary *)getAttributes:(NSArray *)attributes
{
    NSMutableDictionary *mutableDic = [NSMutableDictionary dictionaryWithCapacity:0];
    for (id object in attributes) {
        if ([object isKindOfClass:[UIColor class]]) {
            [mutableDic setObject:object forKey:NSForegroundColorAttributeName];
        }
        if ([object isKindOfClass:[UIFont class]]) {
            [mutableDic setObject:object forKey:NSFontAttributeName];
        }
    }
    return mutableDic;
}







+(void)showLeftTitleDestructiveAlertControllerTitle:(NSString *)title Message:(NSString *)message LeftTitle:(NSString *)title1  rightTile:(NSString *)title2 leftBlock:(leftAlertBlock)leftBlock rightBlock:(rightAlertBlock)rightBlock viewController:(UIViewController *)vc
{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    if (title1) {
        UIAlertAction *action1 = [UIAlertAction actionWithTitle:title1 style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            leftBlock(action);
        }];
        [alert addAction:action1];

    }
    if (title2) {
        UIAlertAction *action2 = [UIAlertAction actionWithTitle:title2 style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            rightBlock(action);
        }];
        [alert addAction:action2];
    }
    
    [vc presentViewController:alert animated:YES completion:NULL];
}

+(void)showActionSheetControllerTitle:(NSString *)title Message:(NSString *)message title1:(NSString *)title1  title2:(NSString *)title2 cancel:(NSString *)title3 block1:(leftAlertBlock)leftBlock block2:(rightAlertBlock)rightBlock cancelBlock:(cancelAlertBlock)cancelBlock viewController:(UIViewController *)vc
{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:title1 style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        leftBlock(action);
    }];
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:title2 style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        rightBlock(action);
    }];
    UIAlertAction *action3 = [UIAlertAction actionWithTitle:title3 style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        cancelBlock(action);
    }];
    [alert addAction:action1];
    [alert addAction:action2];
    [alert addAction:action3];
    [vc presentViewController:alert animated:YES completion:NULL];
}



+(UIAlertController *)showTextFieldDestructiveLeftTitleAlertControllerTitle:(NSString *)title Message:(NSString *)message LeftTitle:(NSString *)title1  rightTile:(NSString *)title2 leftBlock:(leftTextAlertBlock)leftBlock rightBlock:(rightTextAlertBlock)rightBlock placeholder:(NSString *)placeholder  text:(NSString *)text viewController:(UIViewController *)vc
{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    WeakObj(alert);
    
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:title1 style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        leftBlock(action,alertWeak.textFields);
    }];
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:title2 style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        rightBlock(action,alertWeak.textFields);
    }];
    WeakObj(placeholder);
    WeakObj(text);
    WeakObj(vc);
    [alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        textField.placeholder = placeholderWeak;
        textField.text = textWeak;
        textField.delegate = (id)vcWeak;
    }];
    [alert addAction:action1];
    [alert addAction:action2];
    if (text.length == 0) {
        action1.enabled = NO;
    }else
    {
        action1.enabled = YES;
    }
    [vc presentViewController:alert animated:YES completion:NULL];
    return alert;
}


+(void)showCancelTitleAlertControllerTitle:(NSString *)title Message:(NSString *)message title:(NSString *)title1 leftBlock:(leftAlertBlock)leftBlock viewController:(UIViewController *)vc
{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:title1 message:message preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:title style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        leftBlock(action);
    }];

    [alert addAction:action1];
    [vc presentViewController:alert animated:YES completion:NULL];
}

+(NSString *)getDateStringWithDateSince1970:(NSTimeInterval)time format:(NSString *)format
{
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:time/1000.000];
    NSDateFormatter *formatDate = [[NSDateFormatter alloc] init];
    [formatDate setDateFormat:format];
    return [formatDate stringFromDate:date];
}

+(NSString *)getDateStringWithDate:(NSDate *)date format:(NSString *)format
{
    
    NSDateFormatter *formatDate = [[NSDateFormatter alloc] init];
    [formatDate setDateFormat:format];
    return [formatDate stringFromDate:date];
}

+(void)setView:(UIView *)view BordeWidth:(CGFloat)width cornerRadius:(CGFloat)cornerRadius bordeColor:(UIColor *)color maskToBounds:(BOOL)maskToBounds
{
    view.layer.borderWidth = width;
    view.layer.cornerRadius = cornerRadius;
    view.layer.borderColor = color.CGColor;
    view.layer.masksToBounds = maskToBounds;
}

+(void)showActionSheetControllerTitle:(NSString *)title Message:(NSString *)message title1:(NSString *)title1  title2:(NSString *)title2 title3:(NSString *)title3 block1:(leftAlertBlock)leftBlock block2:(rightAlertBlock)rightBlock cancelBlock:(cancelAlertBlock)cancelBlock viewController:(UIViewController *)vc
{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:title1 style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        leftBlock(action);
    }];
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:title2 style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        rightBlock(action);
    }];
    UIAlertAction *action3 = [UIAlertAction actionWithTitle:title3 style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        cancelBlock(action);
    }];
    [alert addAction:action1];
    [alert addAction:action2];
    [alert addAction:action3];
    [vc presentViewController:alert animated:YES completion:NULL];
}

//获取当前屏幕显示的viewcontroller
+ (UIViewController *)getCurrentVC
{
    UIViewController *result = nil;
    
    UIWindow * window = [[UIApplication sharedApplication] keyWindow];
    if (window.windowLevel != UIWindowLevelNormal)
    {
        NSArray *windows = [[UIApplication sharedApplication] windows];
        for(UIWindow * tmpWin in windows)
        {
            if (tmpWin.windowLevel == UIWindowLevelNormal)
            {
                window = tmpWin;
                break;
            }
        }
    }
    
    UIView *frontView = [[window subviews] objectAtIndex:0];
    id nextResponder = [frontView nextResponder];
    
    if ([nextResponder isKindOfClass:[UIViewController class]])
        result = nextResponder;
    else
        result = window.rootViewController;
    
    return result;
}
@end
